package com.flower.practice.config.context;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-21 16:42
 */
public class LoginContextHolder {

//    private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL = new ThreadLocal<>();


    private static final InheritableThreadLocal<Map<String, Object>> THREAD_LOCAL = new InheritableThreadLocal<>();


//    private static final TransmitThreadLocal<Map<String, Object>> TRANSMIT_THREAD_LOCAL = new TransmitThreadLocal<>();



    public static void set(String key,Object val){
        Map<String, Object> threadLocalMap = getThreadLocalMap();
        threadLocalMap.put(key,val);
    }

    public static Object get(String key){
        Map<String, Object> threadLocalMap = getThreadLocalMap();
        return threadLocalMap.get(key);
    }

    public static Map<String, Object> getThreadLocalMap() {

        Map<String, Object> map = THREAD_LOCAL.get();
        if(Objects.isNull(map)){
            map = new ConcurrentHashMap<>();
            THREAD_LOCAL.set(map);
        }

        return map;
    }


    public static void remove(){
        THREAD_LOCAL.remove();
    }

    public static String getLonginId(){
        return (String) getThreadLocalMap().get("loginId");
    }

}
